package com.mo.modular.auth.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.mo.common.exception.BizExceptionEnum;
import com.mo.common.exception.BussinessException;
import com.mo.config.properties.JwtProperties;
import com.mo.core.util.JwtTokenUtil;
import com.mo.dto.AuthRequest;
import com.mo.modular.auth.controller.dto.AuthResponse;

/**
 * 请求验证的
 *
 * @author chenxiaodong
 * @Date 2017/8/24 14:22
 */
@RestController
public class AuthController {

//    @Autowired
//    private JwtTokenUtil JwtTokenUtil;

	@Autowired
	private JwtProperties jwtProperties;
//	@Autowired
//	private IMuserService muserService;

    @RequestMapping(value = "${jwt.auth-path}")
    public ResponseEntity<?> createAuthenticationToken(AuthRequest authRequest) {

        boolean validate = true;//muserService.validate(authRequest);

        if (validate) {
            final String randomKey = JwtTokenUtil.getRandomKey();
            final String token = JwtTokenUtil.generateToken(authRequest.getUserName(), randomKey, jwtProperties.getMd5Key(), jwtProperties.getSecret(), jwtProperties.getExpiration());
            return ResponseEntity.ok(new AuthResponse(token, randomKey));
        } else {
            throw new BussinessException(BizExceptionEnum.AUTH_REQUEST_ERROR);
        }
    }
}
